/*
** Text-Based Gaming System 
** Copyright (C) 2008 Eric Day
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to:
** Free Software Foundation, Inc.
** 51 Franklin Street, Fifth Floor,
** Boston, MA  02110-1301, USA
*/

#ifndef _TGSD_H
#define _TGSD_H

#include "tgs_common.h"

typedef struct _TGSD              TGSD;
typedef struct _TGSD_CON          TGSD_CON;
typedef struct _TGSD_GAME         TGSD_GAME;
typedef struct _TGSD_GAME_SESSION TGSD_GAME_SESSION;

/* macros to help with error checking */
#define TGSD_RET_CHECK(__check, __func, __errno) { \
    if (__check) \
    { \
        if (__errno > 0) \
        { \
            fprintf(stderr, __func ":%d:%s\n", __errno, strerror(__errno)); \
            return __errno; \
        } \
        fprintf(stderr, __func ":%d\n", __errno); \
        return -1; \
    } }

#define TGSD_TOK_CHECK(__con, __tok, ...) { \
    if ((__tok) == NULL) \
    { \
        lpc_printf((__con)->lpc, __VA_ARGS__); \
        return 0; \
    } }

/* global variables */
extern TGSD_GAME tgsd_draw;
extern TGSD_GAME tgsd_sudoku;

/*
** structs
*/

struct _TGSD
{
    uint16_t           port;
    TGSD_CON          *con;
    int                con_num;
    int                con_size;
    struct pollfd     *pfd;
    TGSD_GAME_SESSION *gs;
    int                gs_num;
    int                gs_size;
};

struct _TGSD_CON
{
    LPC               *lpc;
    char               name[TGS_NAME_LEN];
    TGSD_GAME_SESSION *gs;
};

struct _TGSD_GAME
{
    char  *name;
    int  (*start)(TGSD_CON *con);
    int  (*join)(TGSD_CON *con);
    int  (*part)(TGSD_CON *con);
    int  (*data)(TGSD_CON *con, char *buf);
};

struct _TGSD_GAME_SESSION
{
    TGSD_GAME *game;
    void      *data;
};

#endif /* _TGSD_H */
